﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp;
using Abp.Authorization;
using Abp.BackgroundJobs;
using Abp.IO.Extensions;
using Abp.Runtime.Session;
using Abp.UI;
using Abp.Web.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using pandx.Mulan.Authorization;
using pandx.Mulan.Authorization.Users.Importing;
using pandx.Mulan.Authorization.Users.Importing.Dto;
using pandx.Mulan.Helpers;
using pandx.Mulan.Storage;

namespace pandx.Mulan.Controllers
{
    public abstract class UserControllerBase : MulanControllerBase
    {
     
        private readonly FileUploadHelper _fileUploadHelper;
        private readonly IHttpContextAccessor _httpContextAccessor;

        protected UserControllerBase(FileUploadHelper fileUploadHelper, IHttpContextAccessor httpContextAccessor)
        {
            _fileUploadHelper = fileUploadHelper;
            _httpContextAccessor = httpContextAccessor;
        }

        [AbpAuthorize(AppPermissions.Pages_Administration_Users_Import)]
        [HttpPost]
        public async Task<JsonResult> ImportFromExcel()
        {
            try
            {
                var results = new List<UploadFileResult>();
                var context = _httpContextAccessor.HttpContext;
                await _fileUploadHelper.UploadPartialFile(context, results, SaveType.B);
                return Json(results);
            }
            catch (UserFriendlyException exception)
            {
                return Json(new AjaxResponse(new ErrorInfo(exception.Message)));
            }
        }
    }
}